﻿﻿var app = angular.module('stuApp', ['ngResource', 'ngCookies','toastr']);
app.config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
}]);
app.config(function (toastrConfig) {
    angular.extend(toastrConfig, {
        autoDismiss: false,
        containerId: 'toast-container',
        maxOpened: 2,
        newestOnTop: true,
        positionClass: 'toast-center-center',
        preventDuplicates: false,
        preventOpenDuplicates: false,
        target: '#passbox',
        timeOut: 1000,
    });
});
app.controller('passCtr', function ($scope, passService, $location, $filter, $cookieStore, toastr) {

    $scope.topicList = [];
    $scope.topic = {};
    $scope.score = 0;
    $scope.rate = 0;
    $scope.modal_info = {
        "info": "",
        "ishide": "",
    };
    $scope.savelist = [];

    //cookie
    $scope.getCookie = function () {
        try {
            //获取用户信息
            $scope.userid = $cookieStore.get('loginInfo').empid;
            $scope.username = $cookieStore.get('loginInfo').name;
            var type = $cookieStore.get('loginInfo').usertype;
            if (type === 1) {
                window.location.href = "/login.html";
            }
        }
        catch (e) {
            //获取用户信息失败则返回登录页面
            window.location.href = "/login.html";
        }
    }
    //加载
    $scope.init = function () {
        $scope.start_date = new Date();
        $scope.chapterid =$location.search().chapterid;
        $scope.getCookie();
        $scope.do_search();
        $scope.num = 1;
        $scope.isdisable_pre = true;
    }
    //数据获取
    $scope.do_search = function () {
        //if ($scope.chapterid == 0) {
            //巩固

       // } else {
            //闯关
            passService.Getexercises_pass($scope.chapterid, function (data) {
                $scope.topicList = data.data;
                $scope.topic = $scope.topicList[0];
                $scope.total = $scope.topicList.length;
            })
        //}
    }
    //模态框
    $scope.modal_close = function () {
        $("#warning").modal('hide');  
    }
    $scope.modal_define = function () {
        window.location.href = "../../../student/Index#";
    }
    //再来一次
    $scope.again = function () {
        if ($cookieStore.get("again_type") != 1) {
            if ($scope.rate < 60) {
                $cookieStore.put("again_type", 0);
            } else {
                $cookieStore.put("again_type", 1);
            }
        }
        window.location.reload();
    }
    //查看解析
    $scope.view_parsing = function () {
        window.location.href = '../../../student/Index#/parse?chapter_id=' + $scope.chapterid;
    }
    //退出
    $scope.exit = function () {
        $scope.modal_info = {
            "info": "是否确认退出?",
            "ishide": false,
        }
        $("#warning").modal('show');
    }
    //上一题
    $scope.pre_question = function () {
        $scope.topic = $scope.topicList[$scope.num - 2];
        $scope.num = $scope.num-1;
        if ($scope.num == 1) {
            $scope.isdisable_pre = true;
        } else {
            $scope.isdisable_pre = false;
        }
    }
    //下一题
    $scope.next_question = function () {
        if ($scope.topic.index != null) {
            $scope.topic = $scope.topicList[$scope.num];
            $scope.num = $scope.num + 1;
            $scope.isdisable_pre = false;
        }
        else {
            toastr.warning("请先完成当前题目！");
        }
    }
    //完成闯关
    $scope.complete = function () {
        if ($scope.topic.index != null) {
            $scope.end_date = new Date();
            var _time = $scope.end_date - $scope.start_date;
            $scope.time = $filter('date')(_time, 'mm:ss');
            $scope.set_score();
        }
        else {
            toastr.warning("请先完成当前题目！");
        }
    }
    //选择
    $scope.chooseOption = function (index) {
        $scope.topic.index = index;
        $scope.saveinfo = {
            "student_id": $cookieStore.get('loginInfo').empid,
            "exercises_id": $scope.topic.id,
            "results": $scope.topic.exerselect[index].id,
            "results_name": $scope.topic.exerselect[index].name,
            "chapter_id": $scope.chapterid,
        }
        if ($scope.savelist[$scope.num - 1]) {
            $scope.savelist[$scope.num - 1]=$scope.saveinfo;
        } else {
            $scope.savelist.push($scope.saveinfo);
        }
    }
    //分数
    $scope.set_score = function () {
        $scope.integral = 0;//积分
        $scope.score = 0;//分数
        $scope.score_num = 0;//正确的题数
        for (var i = 0; i < $scope.topicList.length; i++) {
            if ($scope.savelist[i].results_name == $scope.topicList[i].exeranswer[0].answername) {
                $scope.score += $scope.topicList[i].score;
                $scope.score_num++;
                $scope.savelist[i].istrue = 1;
                $scope.savelist[i].exercises_number = $scope.topicList[i].score;
            } else {
                $scope.savelist[i].istrue = 0;
                $scope.savelist[i].exercises_number = 0;
            }
        }
        $scope.rate = ($scope.score_num / $scope.total) * 100;

        //积分
        $scope.integral = $scope.rate;
        if ($cookieStore.get('again_type') == 0) {   //重新闯关
            var cookie_integral = $cookieStore.get('integral');
            if ($cookieStore.get('integral') == null) { 
                $cookieStore.put('integral', $scope.integral);
            }else if (cookie_integral + $scope.integral > 100) {
                $scope.integral = 100 - cookie_integral;
                $cookieStore.put('integral', 100);
            } else {
                $cookieStore.put('integral', cookie_integral + $scope.integral);
            }
        } else if ($cookieStore.get('again_type') == 1) { //再来一次
            var cookie_integral_again = $cookieStore.get('integral_again');
            if (cookie_integral_again == null) {
                $cookieStore.put('integral_again', $scope.integral);
            }
            else if (cookie_integral_again + $scope.integral > 100) {
                $scope.integral = 100 - cookie_integral_again;
                $cookieStore.put('integral_again', 100);
            } else {
                $cookieStore.put('integral_again', cookie_integral_again + $scope.integral);
            }
        }

        $("#example").modal('show');
        $scope.save();
    }

    //提交
    $scope.save = function () {
        var par = {
            "json": JSON.stringify($scope.savelist),
        }
        passService.Getpassresult(par, function (data) {
            if (data.isSuccess == true) {

            }
        })
    }
    $scope.init();
})